package com.dc.config;

import com.dc.config.http.MyRequestInterceptor;
import com.dc.config.http.MyRestTemplateCustomizer;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.client.RootUriTemplateHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriTemplateHandler;

import javax.annotation.Resource;
import java.time.Duration;

@Configuration(proxyBeanMethods = false)
public class HttpConfig {

//    private final String rootUri = "http://energy.yiulian.com:8089";
    @Resource
    private CommonConfig commonConfig;

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        UriTemplateHandler uriTemplateHandler = new RootUriTemplateHandler(commonConfig.getHost());
        return builder
                .uriTemplateHandler(uriTemplateHandler)
                .setReadTimeout(Duration.ofSeconds(60L))
//                .setConnectTimeout(Duration.ofSeconds(60L))
                .build();
    }

    @Bean
    public MyRequestInterceptor myRequestInterceptor() {
        return new MyRequestInterceptor();
    }

    @Bean
    public MyRestTemplateCustomizer restTemplateCustomizer() {
        return new MyRestTemplateCustomizer();
    }

//    @Bean
//    @DependsOn("restTemplateCustomizer")
//    public RestTemplateBuilder restTemplateBuilder(RestTemplateCustomizer restTemplateCustomizer) {
//        return new RestTemplateBuilder(restTemplateCustomizer)
//                .basicAuthentication(emqxConfig.getBasicUsername(), emqxConfig.getBasicPassword())
//                .setConnectTimeout(Duration.ofSeconds(5))
//                .setReadTimeout(Duration.ofSeconds(2));
//    }
}
